package com.javabasic.algorithm.sword_of_offer;

/**
 * @author mir.xiong
 * @version 1.0
 * @description
 * @see
 * @since Created by work on 2022/1/17 11:49 下午
 */
public class 剑指_Offer_58_I_翻转单词顺序 {

    /**
     * 正则表达式匹配+反转
     * @param s
     * @return
     */
    public String reverseWords(String s) {
        String trim = s.trim();
        trim = trim.replaceAll("\\s+"," ");
        System.out.println(trim);
        String[] array = trim.split(" ");
        int len = array.length;
        StringBuilder result = new StringBuilder();
        for (int i = len-1; i >= 0; i--) {
            result.append(array[i]);
            if (i != 0) {
                result.append(" ");
            }
        }
        return result.toString();
    }

    /**
     * 正则表达式匹配+反转
     * @param s
     * @return
     */
    public String reverseWords2(String s) {
        String[] array = s.trim().split("\\s+");
        int len = array.length;
        StringBuilder result = new StringBuilder();
        for (int i = len-1; i >= 0; i--) {
            result.append(array[i]);
            if (i != 0) {
                result.append(" ");
            }
        }
        return result.toString();
    }


}
